#include <iostream>
#include <unordered_map>
#include <fstream>
#include <vector>
//#include <string>

const char *unknow = "unknow";
std::string defaultpath = "./resourse/dict.txt";
const std::string sep = " ";

class Translate
{


public:
    Translate(std::string& dictname = defaultpath):dict_path(dictname)
    {
        DownLown();
        Resolution();
    }
    //加载
    void DownLown()
    {
        std::fstream in(dict_path);
        //按行读取
        std::string line;
        while(std::getline(in,line))
        {
            lines.push_back(line);
        }
    }
    //解析,制作字典
    void Resolution()
    {
        for(auto& line : lines)
        {
            auto pos = line.find(sep);
            std::string word = line.substr(0,pos);
            std::string chinese = line.substr(pos + sep.size());
            dict.insert(std::make_pair(word,chinese));
        }
    }

    void Debug()
    {
        for(auto& c : lines)
        {
            std::cout << c << std::endl;
        }

        for(auto& w : dict)
        {
            std::cout << w.first << ":" << w.second << std::endl;
        }
    }
    std::string Execut(const std::string &word)
    {
        // 先在字典里查找有无word
        auto iter = dict.find(word);
        if (iter == dict.end())
            return unknow;
        return dict[word];
    }
    ~Translate()
    {

    }

private:
    std::string dict_path;
    std::unordered_map<std::string, std::string> dict;
    std::vector<std::string> lines;
};